<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js异步编程之Promise队列</title>
  <style>
    li {
      margin-bottom: 10px;
    }
    li button {
      cursor: pointer;
    }
  </style>
  <script src="./ajax.js"></script>
</head>
<body>
  <ul>
    <li><button onclick="promiseQueue()">测试promiseQueue</button> Promise队列，即让多个Promise按顺序依次执行，当前Promise执行必须依赖于上一个Promise的状态</li>
    <li><button onclick="mapQueue([1, 2, 3, 4])">测试mapQueue</button> <button onclick="mapPromiseQueue([promise1, promise2])">测试mapPromiseQueue</button> Promise队列可使用map来实现</li>
    <li><button onclick="reduceQueue([1, 2, 3, 4])">测试reduceQueue</button> <button onclick="reducePromiseQueue([promise1, promise2])">测试reducePromiseQueue</button> Promise队列可使用reduce来实现</li>
    <li><button onclick="renderByQueue()">测试renderByQueue</button> 使用Promise队列渲染数据</li>
  </ul>
  <script>
    // Promise队列
    function promiseQueue() {
      let promise = Promise.resolve()
      promise = promise.then(res => {
        return new Promise(resolve => {
          console.log('1')
          resolve()
        })
      })
      promise.then(res => {
        return new Promise(resolve => {
          setTimeout(() => {
            console.log('2')
            resolve()
          }, 1000)
        })
      })
    }
    // 使用map封装
    function mapQueue(nums) {
      let promise = Promise.resolve()
      nums.map((item, index) => {
        promise = promise.then(_ => {
          return new Promise(resolve => {
            if (index) {
              setTimeout(() => {
                console.log(item)
                resolve()
              }, 1000)
            } else {
              console.log(item)
              resolve()
            }
          })
        })
      })
    }
    function mapPromiseQueue(promises) {
      let promise = Promise.resolve()
      promises.map(item => {
        promise = promise.then(_ => item())
      })
    }
    function promise1() {
      return new Promise(resolve => {
        console.log('p1')
        resolve()
      })
    }
    function promise2() {
      return new Promise(resolve => {
        setTimeout(() => {
          console.log('p2')
          resolve()
        }, 1000)
      })
    }
    // 使用reduce封装
    function reduceQueue(nums) {
      nums.reduce((promise, item, index) => {
        return promise.then(_ => {
          return new Promise(resolve => {
            if (index) {
              setTimeout(() => {
                console.log(item)
                resolve()
              }, 1000)
            } else {
              console.log(item)
              resolve()
            }
          })
        })
      }, Promise.resolve())
    }
    function reducePromiseQueue(promises) {
      promises.reduce((promise, item) => {
        return promise.then(_ => item())
      }, Promise.resolve())
    }
    // 使用Promise队列渲染数据
    function renderByQueue(users) {
      reducePromiseQueue(['miracle', 'jack'].map((item, index) => {
        return () => ajax(`${location.href.replace('index.html', item + '.json')}?name=${item}`).then(res => {
          const render = () => {
            const elem = document.createElement('h2')
            elem.innerHTML = res.name
            document.body.appendChild(elem)
          }
          if (index) {
            setTimeout(() => render(), 1000)
          } else {
            render()
          }
        })
      }))
    }
  </script>
</body>
</html>
